package userInterface;

import core.demo.BubbleSort;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;


public class InputDialog extends JFrame implements ItemListener, ActionListener {
    private int[] numbers;
    private int sortIndex;//1.bub,2.choose,3.quick
    private JTextField jTextField;
    private JLabel inputL;
    private JButton confirm;
    private JRadioButton bub,choose,quick;
    private ButtonGroup group;

    public InputDialog(){
        super("排序选择");
        setUI();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public int[] getNumbers() {
        return numbers;
    }

    public int getSortIndex() {
        return sortIndex;
    }

    public void setUI(){
        //元素初始化
        inputL=new JLabel("请输入待排序的数列");
        inputL.setBounds(80,30,150,30);
        jTextField=new JTextField(SwingConstants.CENTER);
        jTextField.setBounds(25,100,240,30);
        bub=new JRadioButton("冒泡排序");
        bub.setBounds(0,200,80,30);
        choose=new JRadioButton("选择排序");
        choose.setBounds(100,200,80,30);
        quick=new JRadioButton("快速排序");
        quick.setBounds(200,200,80,30);
        group=new ButtonGroup();
        group.add(choose);
        group.add(bub);
        group.add(quick);
        confirm=new JButton("确认");
        confirm.setBounds(100,300,100,30);
        //添加监听
        bub.addItemListener(this);
        choose.addItemListener(this);
        quick.addItemListener(this);
        confirm.addActionListener(this);
        //todo
        setVisible(true);
        setSize(300,400);
        setLayout(null);
        setLocationRelativeTo(null);
        //添加组件
        add(inputL);
        add(jTextField);
        add(bub);
        add(choose);
        add(quick);
        add(confirm);
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getSource()==bub)
        {
            System.out.println("切换冒泡");
           sortIndex=1;
        }
        if(e.getSource()==choose)
        {
            System.out.println("切换选择");
            sortIndex=2;
        }
        if(e.getSource()==quick)
        {
            System.out.println("切换快排");
            sortIndex=3;
        }
    }

    public void substr(String str) throws NumberFormatException//对jtextfield的数字进行切割
    {
        int count=0;
        for (String temp:str.split(" "))
        {
                if (!temp.equals(""))
                {
                   count++;
                }
        }
        numbers=new int[count];
        count=0;
        for (String temp:str.split(" "))
        {
            if (!temp.equals(""))
            {
                numbers[count++]=Integer.parseInt(temp);
            }
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==confirm)
        {
            System.out.println("点击确认按钮");
            try {
                String str=this.jTextField.getText();
                substr(str);//进行字符串切割
                this.dispose();
                MainWindow mainWindow=new MainWindow(this);
            }catch (NumberFormatException n)
            {
                JOptionPane.showMessageDialog(this,"存在非法字符，请重新输入！","警告",JOptionPane.WARNING_MESSAGE);
            }

        }
    }
}
